<?php
/*
 * @Author: your name
 * @Date: 2020-11-16 14:19:04
 * @LastEditTime: 2020-12-29 15:00:16
 * @LastEditors: Please set LastEditors
 * @Description: 防火墙 规则预览
 * @FilePath: /special/application/adminapiv3/service/module_manage/netfilter/RulePreviewService.php
 */
namespace app\adminapiv3\service\module_manage\netfilter;

use app\adminapiv3\model\NetfilterFirewallRuleModel;
use app\adminapiv3\service\BaseService;
use app\common\definition\HostDefinition;
use app\common\exception\CustomException;
use app\common\definition\netfilter\FirewallRuleDefinition;
use think\Db;

class RulePreviewService extends BaseService
{
    /**
     * @name: 列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-16 14:20:22
     */
    public function list ($input)
    {
        $data = Db::name('netfilter_firewall_rule')->where('host_id', $input['host_id'])->order('rank', 'ASC')->select();

        $os = $this->getHost('os', 'string');
        $SortRuleService = new SortRuleService();
        $data = $SortRuleService->addLogRule($input['host_id'], $data);
        $data = $SortRuleService->addDisguisePortRule($input['host_id'], $os, $data);
        $return = $this->addShowName($data);
        $SortRuleService = new SortRuleService();
        if ($os == HostDefinition::LINUX_OPERATION_SYSTEM)
        {
            $return = $SortRuleService->sortRule($return);
        }
        
        return $return;
    }

    /**
     * @name: 排序
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-16 14:20:48
     */
    public function sort ($input)
    {
        //添加用户操作日志
        global $logArray;
        $host_name_s = $this->getHost('name', 'string');
        $logArray = array(
            'content' => '用户自定义排序主机规则',
            'new_info' => '主机:' . $host_name_s . '用户自定义排序规则'
        );

        $id_a = explode(',', $input['id']);
        $host_id = $input['host_id'];
        foreach ($id_a as $rank => $id)
        {
            $saveData[] = [
                'id' => $id,
                'host_id' => $host_id,
                'rank' => $rank + 1
            ];
        }
        $NetfilterFirewallRuleModel = new NetfilterFirewallRuleModel();
        $NetfilterFirewallRuleModel->ruleSort($host_id, $saveData);
    }

    /**
     * @name: 推送规则
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-25 13:10:38
     */
    public function push ($host_id)
    {
        //添加用户操作日志
        global $logArray;
        $host_name_s = $this->getHost('name', 'string');
        $logArray = array(
            'content' => '推送规则到客户端',
            'new_info' => '主机:' . $host_name_s . '推送规则到客户端'
        );

        $return = Db::name('host_link')->where('host_id', $host_id)->where('module', 'netfilter')->find();

        if (empty($return))
        {
            throw new CustomException('未找到该主机', '301');
        }

        if($return['status'] != 1)
        {
            throw new CustomException('该主机不在线', '302');
        }

    }

    /**
     * @name: 排序防火墙规则
     * @Param: 
     * @Description: 仅用于本地排序组件返回数据修改防火墙数据的排序和所属区域
     * @Author: foo
     * @Date: 2020-11-30 16:46:49
     */
    public function rankFirewallRule ($input)
    {
        foreach ($input['iptrules'] as $rank => $insert_data)
        {
            $input['iptrules'][$rank]['rank'] = $rank;
        }
        
        $NetfilterFirewallRuleModel = new NetfilterFirewallRuleModel();
        $NetfilterFirewallRuleModel->ruleSort($input['host_id'], $input['iptrules']);
    }

    /**
     * @name: 增加展示名
     * @Param: 
     * @Description: 增加规则预览列表的规则来源名和规则类型名
     * @Author: foo
     * @Date: 2020-12-03 16:05:44
     */
    public static function addShowName ($rule_a)
    {
        $rule_a = $rule_a ? $rule_a : [];
        $return = array_map(function($rule){
            
            switch ($rule['source']){
                case FirewallRuleDefinition::ORIGIN_RULE_SOURCE :
                    $rule['source_name'] = FirewallRuleDefinition::ORIGIN_RULE_SOURCE_NAME;
                break;
                case FirewallRuleDefinition::CONNECT_RULE_SOURCE :
                    $rule['source_name'] = FirewallRuleDefinition::CONNECT_RULE_SOURCE_NAME;
                break;
                case FirewallRuleDefinition::HANDLE_RULE_SOURCE :
                    $rule['source_name'] = FirewallRuleDefinition::HANDLE_RULE_SOURCE_NAME;
                break;
                case FirewallRuleDefinition::FIREWALL_SET_RULE_SOURCE :
                    $rule['source_name'] = FirewallRuleDefinition::FIREWALL_SET_RULE_SOURCE_NAME;
                break;
                case FirewallRuleDefinition::FIREWALL_LOG_RULE_SOURCE :
                    $rule['source_name'] = FirewallRuleDefinition::FIREWALL_LOG_RULE_SOURCE_NAME;
                break;
                case FirewallRuleDefinition::DISGUISE_PORT_RULE_SOURCE :
                    $rule['source_name'] = FirewallRuleDefinition::DISGUISE_PORT_RULE_SOURCE_NAME;
                break;
                default :
                    $rule['source_name'] = FirewallRuleDefinition::UNKNOW_RULE_SOURCE_NAME;
            }
            
            switch ($rule['region']){
                case FirewallRuleDefinition::FIREWALL_DEFAULT_CONFIG_RULE_REGION :
                    $rule['region_name'] = FirewallRuleDefinition::FIREWALL_DEFAULT_CONFIG_RULE_REGION_NAME;
                break;
                case FirewallRuleDefinition::FIREWALL_NEW_LINK_RULE_REGION :
                    $rule['region_name'] = FirewallRuleDefinition::FIREWALL_NEW_LINK_RULE_REGION_NAME;
                break;
                case FirewallRuleDefinition::CONNECT_TOP_RULE_REGION :
                    $rule['region_name'] = FirewallRuleDefinition::CONNECT_TOP_RULE_REGION_NAME;
                break;
                case FirewallRuleDefinition::CONNECT_NORMAL_RULE_REGION :
                    $rule['region_name'] = FirewallRuleDefinition::CONNECT_NORMAL_RULE_REGION_NAME;
                break;
                case FirewallRuleDefinition::INTRANET_HEAD_RULE_REGION :
                    $rule['region_name'] = FirewallRuleDefinition::INTRANET_HEAD_RULE_REGION_NAME;
                break;
                case FirewallRuleDefinition::INTRANET_NORMAL_RULE_REGION :
                    $rule['region_name'] = FirewallRuleDefinition::INTRANET_NORMAL_RULE_REGION_NAME;
                break;
                case FirewallRuleDefinition::INTRANET_TAIL_RULE_REGION :
                    $rule['region_name'] = FirewallRuleDefinition::INTRANET_TAIL_RULE_REGION_NAME;
                break;
                case FirewallRuleDefinition::EXTRANET_HEAD_RULE_REGION :
                    $rule['region_name'] = FirewallRuleDefinition::EXTRANET_HEAD_RULE_REGION_NAME;
                break;
                case FirewallRuleDefinition::INTRANET_TAIL_RULE_REGION :
                    $rule['region_name'] = FirewallRuleDefinition::INTRANET_TAIL_RULE_REGION_NAME;
                break;
                case FirewallRuleDefinition::EXTRANET_NORMAL_RULE_REGION :
                    $rule['region_name'] = FirewallRuleDefinition::EXTRANET_NORMAL_RULE_REGION_NAME;
                break;
                case FirewallRuleDefinition::EXTRANET_TAIL_RULE_REGION :
                    $rule['region_name'] = FirewallRuleDefinition::EXTRANET_TAIL_RULE_REGION;
                break;
                case FirewallRuleDefinition::DEFAULT_HEAD_RULE_REGION :
                    $rule['region_name'] = FirewallRuleDefinition::DEFAULT_HEAD_RULE_REGION_NAME;
                break;
                case FirewallRuleDefinition::DEFAULT_NORMAL_RULE_REGION :
                    $rule['region_name'] = FirewallRuleDefinition::DEFAULT_NORMAL_RULE_REGION_NAME;
                break;
                case FirewallRuleDefinition::DEFAULT_TAIL_RULE_REGION :
                    $rule['region_name'] = FirewallRuleDefinition::DEFAULT_TAIL_RULE_REGION_NAME;
                break;
                case FirewallRuleDefinition::FIREWALL_LOG_REGION :
                    $rule['region_name'] = FirewallRuleDefinition::FIREWALL_LOG_REGION_NAME;
                break;
                default :
                    $rule['region_name'] = FirewallRuleDefinition::UNKNOW_RULE_REGION_NAME;
            }
            return $rule;
        }, $rule_a);

        return $return;
    }

   
}